export default function (routeContext, configContext, callback = null) {
  const router = {}
  const configAry = configContext.keys()
  routeContext.keys().map(routeFileName => {
    let routeItem
    const file = routeFileName.replace(/^\.|\.vue|\.js/g, '')
    const configFileName = '.' + file + '.json'
    const path = file.replace(/\/[^/]+$/g, _ => _ === '/index' ? '' : _)
    const configData = configAry.length && configAry.indexOf(configFileName) !== -1 ? configContext(configFileName) : {}
    const component = configData.meta && configData.meta.sync === true ? routeContext(routeFileName) : () => routeContext(routeFileName)
    if (!configData.meta) { configData.meta = {} }
    routeItem = {
      component,
      name: path,
      path,
      ...configData
    }
    if (!/^https:\/\/|http:\/\//.test(routeItem.path)) {
      router[path] = routeItem
    }
    if (callback) {
      callback(routeItem)
    }
  })
  return router
}
